package com.qiantu.phone.ui.adapter;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.base.DensityUtils;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.widget.SimpleAnimationDraweeView;

public class SelectSceneIconAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    private String mSelectIcon;
    private int itemWidth;

    public SelectSceneIconAdapter(int itemWidth) {
        super(R.layout.scene_icon_item);
        this.itemWidth = itemWidth;
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        SimpleAnimationDraweeView imageView = helper.getView(R.id.icon);
        View iconContainer = helper.getView(R.id.icon_container);
        imageView.setImageURI(item);
        if (item.equals(mSelectIcon)){
            iconContainer.setSelected(true);
        }else {
            iconContainer.setSelected(false);
        }
        iconContainer.setTag(item);
        iconContainer.setOnClickListener(onClickListener);
        RecyclerView.LayoutParams rl = (RecyclerView.LayoutParams) helper.itemView.getLayoutParams();
        rl.height = itemWidth;
        int position = helper.getLayoutPosition();
        if (position == 0 || position == 1 || position == 2){
            rl.leftMargin = 0;
            rl.rightMargin = 0;
            rl.topMargin = DensityUtils.dip2px(getContext(), 12);
            rl.bottomMargin = 0;
        } else if(position == getData().size() - 1 || position == getData().size() - 2 || position == getData().size() - 3){
            rl.leftMargin = 0;
            rl.rightMargin = 0;
            rl.topMargin = 0;
            rl.bottomMargin = DensityUtils.dip2px(getContext(), 12);
        }else{
            rl.leftMargin = 0;
            rl.rightMargin = 0;
            rl.topMargin = 0;
            rl.bottomMargin = 0;
        }
    }

    public String getSelectIcon(){
        return mSelectIcon;
    }

    public void setSelectIcon(String selectIcon) {
        this.mSelectIcon = selectIcon;
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String s = (String) v.getTag();
            if (!s.equals(mSelectIcon)){
                mSelectIcon = (String) v.getTag();
                notifyDataSetChanged();
                onSelectChanged(mSelectIcon);
            }
        }
    };

    public void onSelectChanged(String mSelectIcon){};
}
